define(function (require, exports, module) {
    var $ = require('jquery');


    var angular = require('angular');

    var autoReplyService = angular.module('autoReplyService',[]);

    autoReplyService.factory('addAutoReply',['$rootScope','http','apife','apibe',
        function($rootScope,http,apife,apibe) {
        var config = {
            'welcome' : {
                r : 'auto-reply/get-welcome'
            },
            'autoreply' : {
                r : 'auto-reply/get-auto-reply'
            },
            'keyword' : {
                r : 'auto-reply/get-rule-info'
            }
        }
        var DEFAULT_REPLY_TYPE = 'welcome';

        var obj = {};

        obj.scope = null;

        obj.replyType = DEFAULT_REPLY_TYPE; //"welcome"或"autoreply"或"keyword"，这里welcome为被添加自动回复，autoreply为自动回复,keyword为关键字回复

        obj.currRule = null;

        obj.changReplyType =function(replyType){
            obj.replyType = replyType;
            if(replyType=='keyword'){
                obj.getRuleInfos();
            }else{
                obj.getMsg();
            }
        }

        obj.getMsg = function() {
            http.get(config[obj.replyType].r)
                .success(function (data, status, headers, config) {
                    obj.scope.$broadcast('initTab',data.data);
                });
        }


        obj.postMsg = function(data){
            if(obj.replyType != 'keyword'){
                http.post('auto-reply/add-auto-reply',{type:data.type,reply_type:obj.replyType,content:data.content})
                    .success(function(data, status, headers, config) {
                        $rootScope.$emit('httpRequestSuccess','保存成功！');
                        //alert(data);
                    });
            }else{
                http.post('auto-reply/add-rule-info',{
                    id : obj.currRule.id?obj.currRule.id:null,
                    name : obj.currRule.name,
                    keyword_list : obj.currRule.keyword_data,
                    reply_type : data.type,
                    reply_content : data.content
                })
                    .success(function(data, status, headers, config) {
                        obj.getRuleInfos();
                    });
            }

        };

        obj.getRuleInfos = function(){
            http.get('auto-reply/get-rule-info')
                .success(function (data, status, headers, config) {
                    obj.ruleList = data.data;
                });

        }
        obj.addRuleInfo = function(){
            obj.ruleList=[].concat({
                keyword_data : [],
                edit : true
            },obj.ruleList);
            obj.currRule = obj.ruleList[0];
        }

        obj.addWord = function($dialogScope,$data){
            var rule = $dialogScope.parent.rule;
            var keyword_data = rule.keyword_data;
            keyword_data.push({keyword:$data.keyword,is_exact:0});
        }

        obj.updateWord = function($dialogScope,$data){
            $dialogScope.parent.keyword.keyword = $data.keyword
        };
        obj.editRule = function(rule){
            rule.edit=true;
            obj.currRule = rule;
        };
        obj.sendTabInit = function(){
            if(obj.currRule){
                obj.scope.$broadcast('initTab',obj.currRule.id?obj.currRule:null);
            }
        }
        obj.saveRule = function(rule,keywordForm){
            if(keywordForm){
                keywordForm.$setSubmitted(true);
                if(keywordForm.$invalid || rule.keyword_data.length==0){
                    return;
                }
            }
            obj.currRule = rule;
            obj.scope.$broadcast('tabData');

        }
        obj.deleteRule = function($index,rule){
            if(rule.id){
                http.post('auto-reply/remove-rule',{
                    id : rule.id
                })
                .success(function(data, status, headers, config) {
                    obj.getRuleInfos();
                });
            }else{
                obj.ruleList.splice($index,1);
            }
        }



        obj.resetContent = function(){
            if(obj.scope.sendTab.sendType == 'text'){
                $('.emotion_editor .edit_area').html('');
            }
            obj.currMedia = angular.copy(DEFAULT_CURR_MEDIA);
        }
        obj.save = function(){

            obj.scope.$broadcast('tabData');

        }

        return obj;
    }]);
});